#define DEBUG

#include <stdio.h>
#include "wm_hal.h"

void Error_Handler(void);
static void GPIO_Init(void);
static void SPI_Init(void);
static void PWM_Init(void);

SPI_HandleTypeDef hspi;
PWM_HandleTypeDef hpwm;
uint32_t ch = PWM_CHANNEL_0;

#define data_len (20)
uint8_t tx_data[data_len] = {0};
uint8_t rx_data[data_len] = {0};



int main(void)
{
	SystemClock_Config(CPU_CLK_160M);
	#ifdef DEBUG
	printf("hello world\r\n");
	#endif
	//char testarray[7] = {0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x05}; 
	int i = 0;

	HAL_Init();
	GPIO_Init();
	SPI_Init();
	PWM_Init();
	HAL_PWM_Start(&hpwm, ch);

	for(i = 0; i < data_len; i++)
	{
		tx_data[i] = '1'+i;
	}
	
	while (1)
	{

		printf("GPIOA->AF_SEL is: ");
		for(i = 0; i < 32; i++)
		{
			if(GPIOA->AF_SEL & (0x01 << (31 - i))) printf("1");
			else printf("0");
		}
		printf("\r\n");

		printf("GPIOB->AF_SEL is: ");
		for(i = 0; i < 32; i++)
		{
			if(GPIOB->AF_SEL & (0x01 << (31 - i))) printf("1");
			else printf("0");
		}
		printf("\r\n");

		memset(rx_data, 0, data_len);
		__HAL_SPI_SET_CS_LOW(&hspi);
		HAL_SPI_TransmitReceive(&hspi, tx_data, rx_data, data_len, 1000);
		__HAL_SPI_SET_CS_HIGH(&hspi);

		for (i = 0; i < data_len; i++)
		{
			printf("%c", rx_data[i]);
		}

		printf("\r\n");

		HAL_Delay(300);
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
		HAL_Delay(300);
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
		HAL_Delay(300);
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
		HAL_Delay(300);
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
		HAL_Delay(500);
		HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0 | GPIO_PIN_1);
		HAL_Delay(500);	
		HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0 | GPIO_PIN_1);

		for (i = 0; i < 100; i++)
		{
			HAL_PWM_Duty_Set(&hpwm, ch, i);
			HAL_Delay(20);
		}
		for (i = 99; i >= 0; i--)
		{
			HAL_PWM_Duty_Set(&hpwm, ch, i);
			HAL_Delay(20);
		}
	}
	
    return 0;
}

static void GPIO_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	
	__HAL_RCC_GPIO_CLK_ENABLE();

	GPIO_InitStruct.Pin = GPIO_PIN_1;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0 , GPIO_PIN_RESET);
	

}

static void SPI_Init(void)
{
	hspi.Instance = SPI;
	hspi.Init.Mode = SPI_MODE_MASTER;
	hspi.Init.CLKPolarity = SPI_POLARITY_LOW;
	hspi.Init.CLKPhase = SPI_PHASE_1EDGE;
	hspi.Init.NSS = SPI_NSS_SOFT;
	hspi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_20;
	hspi.Init.FirstByte = SPI_LITTLEENDIAN;
	
	if (HAL_SPI_Init(&hspi) != HAL_OK)
	{
		Error_Handler();
	}
}

static void PWM_Init(void)
{
	// 输出200KHz、占空比40%的波形
	hpwm.Instance = PWM;
	hpwm.Init.AutoReloadPreload = PWM_AUTORELOAD_PRELOAD_ENABLE;
	hpwm.Init.CounterMode = PWM_COUNTERMODE_EDGEALIGNED_DOWN;
	hpwm.Init.Prescaler = 4;
	hpwm.Init.Period = 99;	// 40M / 4 / 100K - 1
	hpwm.Init.Pulse = 19;	// 20% DUTY
	hpwm.Init.OutMode = PWM_OUT_MODE_5SYNC;
	hpwm.Channel = ch;
	
	HAL_PWM_Init(&hpwm);
}


void Error_Handler(void)
{
	while (1)
	{
	}
}

void assert_failed(uint8_t *file, uint32_t line)
{
	printf("Wrong parameters value: file %s on line %d\r\n", file, line);
}
